<?php

/**
 * @Project NUKEVIET 3.x
 * @Author VINADES.,JSC (contact@vinades.vn)
 * @Copyright (C) 2013 VINADES.,JSC. All rights reserved
 * @Createdate Mon, 12 Aug 2013 03:44:28 GMT
 */

if ( ! defined( 'NV_IS_FILE_ADMIN' ) ) die( 'Stop!!!' );

$page_title = "Thêm học sinh";

$my_head = "<script type=\"text/javascript\" src=\"" . NV_BASE_SITEURL . "js/popcalendar/popcalendar.js\"></script>\n";
$my_head .= "<script type=\"text/javascript\" src=\"" . NV_BASE_SITEURL . "js/shadowbox/shadowbox.js\"></script>\n";
$my_head .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . NV_BASE_SITEURL . "js/shadowbox/shadowbox.css\" />\n";
$my_head .= "<script type=\"text/javascript\">\n";
$my_head .= "Shadowbox.init({\n";
$my_head .= "});\n";
$my_head .= "</script>\n";

$contents = "";
$error = "";

$data = array();
$data['hoten'] = filter_text_input( 'hoten', 'post', '' );
$data['ngaysinh'] = filter_text_input( 'ngaysinh', 'post', '', 1, 10 );
unset( $m );
if ( preg_match( "/^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{4})$/", $data['ngaysinh'], $m ) )
{
    $data['ngaysinh1'] = mktime( 0, 0, 0, $m[2], $m[1], $m[3] );
}
else
{
    $data['ngaysinh1'] = "";
}

$data['diachi'] = $nv_Request->get_string( 'diachi', 'post', '' );
   
if ( ($nv_Request->get_int( 'add', 'post', 0 ) == 1) )
{
   if ( $data['hoten'] == "" )
   {
      $error = "Bạn chưa nhập tên học sinh";
   }
   elseif ( $data['ngaysinh'] == "" )
   {
      $error = "Bạn chưa nhập ngày sinh";
   }
   elseif ( $data['diachi'] == "" )
   {
      $error = "Bạn chưa nhập địa chỉ";
   }
   else
   {
      $sql = "SELECT `stt` FROM `" . NV_PREFIXLANG . "_" . $module_data . "` ORDER BY stt DESC LIMIT 0,1";
      $resuilt = $db->sql_query( $sql );
      list ( $currentstt ) = $db->sql_fetchrow( $resuilt );
      $newstt = $currentstt + 1;

      $query = "INSERT INTO `" . NV_PREFIXLANG . "_" . $module_data . "`
      (
         `id`, `stt`, `hoten`, `ngaysinh`, `diachi`
      )
      VALUES
      (
         NULL,
         " . $newstt . ",
         " . $db->dbescape( $data['hoten'] ) . ",
         " . $data['ngaysinh1'] . ",
         " . $db->dbescape( $data['diachi'] ) . "
      )";
      if ( $db->sql_query_insert_id( $query ) )
      {
         $db->sql_freeresult();
         Header( "Location: " . NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name); die();
      }
      else
      {
         $error = "Không thể lưu dữ liệu được";
      }
   
   }
}

if( $error )
{
   $contents .= "<div class=\"quote\" style=\"width: 780px;\">\n
               <blockquote class=\"error\">
                  <span>".$error."</span>
               </blockquote>
            </div>\n
            <div class=\"clear\">
            </div>";
}
$contents .="
<form method=\"post\">
   <table class=\"tab1\">
      <thead>
         <tr>
            <td colspan=\"2\">
               Thông tin học sinh mới
            </td>
         </tr>
      </thead>
      <tbody>
         <tr>
            <td style=\"width: 150px;\">
               Tên học sinh
            </td>
            <td style=\"background: #eee;\">
               <input name=\"hoten\" style=\"width: 470px;\" value=\"" . $data['hoten'] . "\" type=\"text\">
            </td>
         </tr>
      </tbody>
      <tbody>
         <tr>
            <td>
            Ngày sinh
            </td>
            <td>
               <input id=\"ngaysinh\" name=\"ngaysinh\" style=\"width: 470px;\" value=\"" . $data['ngaysinh'] . "\" type=\"text\" />
               <img src=\"" . NV_BASE_SITEURL . "images/calendar.jpg\" style=\"cursor: pointer; vertical-align: middle;\" onclick=\"popCalendar.show(this, 'ngaysinh', 'dd.mm.yyyy', true);\" alt=\"\" height=\"17\" />
            </td>
         </tr>
      </tbody>
      <tbody>
         <tr>
            <td>
            Địa chỉ
            </td>
            <td>
               <input name=\"diachi\" style=\"width: 470px;\" value=\"" . $data['diachi'] . "\" type=\"text\" />
            </td>
         </tr>
      </tbody>
         <tr>
            <td colspan=\"2\" align=\"center\" style=\"background: #eee;\">\n
               <input name=\"confirm\" value=\"Lưu\" type=\"submit\">\n
               <input type=\"hidden\" name=\"add\" value=\"1\">\n
            </td>\n
         </tr>\n
   </table>\n
</form>\n";

include ( NV_ROOTDIR . "/includes/header.php" );
echo nv_admin_theme( $contents );
include ( NV_ROOTDIR . "/includes/footer.php" );

?>